﻿using UnityEngine;
using System.Collections;

public class GameManagerScript : MonoBehaviour {
	
	public GameObject playerPrefab;
	public bool levelCompleted;
	CameraScript cameraScript;
	DataScript data;
	public Renderer DarkMask;
	bool justStarted = true;
	string nextMap;
	public GameObject lineDrawingPrefab;
	
	playerScript myPlayer;
	public goalScript[] myGoals;
	bool handleGameDone = false;
	public TweenScale rewardTarget;
	public moneyPouchScript mpScript;
	
	float gameEndCounter = 0;
	void Awake()
	{
	}
	
	// Use this for initialization
	void Start () 
	{
		//Network.InitializeServer(2, 20003, false);
		data = GameObject.FindGameObjectWithTag("DataManager").GetComponent<DataScript>();
		
		Vector3 tmpPlayerPos = Vector3.zero;
		tmpPlayerPos.x -= 15;
		tmpPlayerPos.y -= 4;
		
		Camera mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
		cameraScript = mainCamera.gameObject.GetComponent<CameraScript>();
		LevelManager.Instance.SetMap();
		LevelAssetManager.Instance.loadAll();
		
		Vector2 playerPos = Vector2.zero;
		bool isOnWorld1 = false;
		
		//turn off player 1 specific using bitwise operation
		
		if(!data.isServer)
		{
			Debug.Log("Is Not Server!");
			LevelManager.Instance.Play(2);
			LevelGrid.Instance.ActivateWorld(2);
		}
		else
		{
			tmpPlayerPos.z = 3;
			Debug.Log("Is Server!");
			LevelManager.Instance.Play(1);
			LevelGrid.Instance.ActivateWorld(1);
			isOnWorld1 = true;
		}
		
		//Look For Spawn Point Object
		XMLData.Map.MapInfo mapInfo = LevelGrid.Instance.mapInfo;
		XMLData.Map.TileObject[] targetList = (isOnWorld1)? mapInfo.tileObj1: mapInfo.tileObj2;
		foreach(XMLData.Map.TileObject to in targetList)
		{
			if(to.objectType == (int)TILEOBJECTS.SPAWNPOINT)
			{
				tmpPlayerPos.x = to.position.x;
				tmpPlayerPos.y = to.position.y;
				tmpPlayerPos.y += 2;
				break;
			}
		}
		nextMap = LevelGrid.Instance.mapInfo.nextMap;
		
		
		Vector3 position =  LevelGrid.Instance.transform.position;
		position.y += 2;
		position.z += 7;
		LevelGrid.Instance.transform.position = position;
		
		GameObject player = Network.Instantiate(playerPrefab,tmpPlayerPos,transform.rotation,0) as GameObject;
		
		Transform tmpTrans = (Network.Instantiate(lineDrawingPrefab,tmpPlayerPos,Quaternion.identity,0) as GameObject).transform;
		//newObject.GetComponent<playerScript>().setAsPartner();
		myPlayer = player.GetComponent<playerScript>();
		myPlayer.latestCheckPoint = tmpTrans;
		int lookIdx = (int)data.selectedCharacter;
		//if(data.isServer)
		//	lookIdx = 1;
		player.GetComponent<playerScript>().initLooks(lookIdx);

		cameraScript.target = player.transform;
	}
	

	// Update is called once per frame
	void Update () {
		if(justStarted)
		{
			Color tmpCol = DarkMask.material.color;
			tmpCol.a -= Time.deltaTime * 0.6f;
			DarkMask.material.color = tmpCol;
			if(tmpCol.a <= 0f)
			{
				tmpCol.a = 0;
				DarkMask.material.color = tmpCol;
				DarkMask.enabled = false;
				justStarted = false;
			}
		}
		//if(Input.GetKeyDown(KeyCode.Y))
		//{
		//	Debug.Log("Next Map is " + nextMap);
		//}
		
		if(handleGameDone)
		{
			Debug.Log("GAME FINSIHED");
			if(nextMap.Length > 4)
			{
				gameEndCounter += Time.deltaTime;
				if(gameEndCounter > 1.0f)
				{
					Color tmpCol = DarkMask.material.color;
					tmpCol.a += Time.deltaTime * 0.6f;
					DarkMask.material.color = tmpCol;
					if(tmpCol.a >= 1.0f)
					{
						data.loadedLevel = nextMap.Substring(0,nextMap.Length - 4);
						Application.LoadLevel("GameScene(Proto)");
					}
				}
			}
			else if(mpScript.selectedCoins)
			{
				
				gameEndCounter += Time.deltaTime;
				if(gameEndCounter > 3.0f)
				{
					mpScript.gameObject.SetActive(false);
					Color tmpCol = DarkMask.material.color;
					tmpCol.a += Time.deltaTime * 0.6f;
					DarkMask.material.color = tmpCol;
					if(tmpCol.a >= 1.0f)
					{
						data.loadedLevel = nextMap.Substring(0,nextMap.Length - 4);
						Network.Disconnect();
						Application.LoadLevel("LobbyScene");
					}
				}
			}
		}
		else
		{	
			bool isGameDone = true;
			for(int i = 0; i < 2; i++)
			{
				if(myGoals[i] == null)
				{
					//Debug.Log("GOAL IS NULL");
					isGameDone = false;
					continue;
				}
				//Debug.Log(myGoals[i].activated);
				if(!myGoals[i].activated)
				{
					isGameDone = false;
				}
			}
			//if(Input.GetKeyDown(KeyCode.Z))
			//	isGameDone = true;
			if(isGameDone)
			{
				myPlayer.setDisableControl(true);
				myPlayer.changeState(States.idle);
				handleGameDone = true;
				if(nextMap.Length < 5)
				{
					rewardTarget.gameObject.SetActive(true);
					rewardTarget.enabled = true;
				}
				DarkMask.enabled = true;
			}
		}
		
	}
	
	
	
}
